package com.nwpu.coursesupport.task;

import com.nwpu.coursesupport.entity.TaskAssignment;
import com.nwpu.coursesupport.enumeration.Status;
import com.nwpu.coursesupport.mapper.TaskAssignmentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * 自动更新任务发布状态
 */
@Component
public class PublishTask {
    @Autowired
    private TaskAssignmentMapper taskAssignmentMapper;

    @Scheduled(cron = "*/30 * * * * ?")   // [秒] [分] [小时] [日] [月] [周] [年]
    public void task() {
        List<TaskAssignment> taskAssignmentList = taskAssignmentMapper.findAll();
        for (TaskAssignment taskAssignment : taskAssignmentList) {
            if (taskAssignment.getPublishDatetime() != null) {
                if (taskAssignment.getPublishDatetime().getTime() <= new Date().getTime()) {
                    taskAssignment.setIsVisible(Status.YES);
                    taskAssignmentMapper.updateAssignStatus(taskAssignment);
                }
            }
        }
    }
}
